package com.yjk.example;

import org.springframework.beans.factory.DisposableBean;
import org.springframework.beans.factory.InitializingBean;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.stereotype.Component;

import java.io.BufferedWriter;
import java.io.File;
import java.io.FileWriter;
import java.io.IOException;
import java.util.Date;

/**
 * @author yujiakui
 * @version 1.0
 * Email: jkyu@haiyi-info.com
 * date: 2018/11/9 9:25
 * description：
 **/
@Component
public class SecondBizService implements Service,InitializingBean,DisposableBean{

    @Autowired
    @Qualifier("thirdService")
    private Service service;


    private Thread curThread;

    public void say(String s) {
        System.out.println("yjkxx " + s);
        service.say(s);
    }

    public void afterPropertiesSet() throws Exception {
        System.out.println("SecondBizService afterPropertiesSet method");
        final BufferedWriter file = new BufferedWriter(new FileWriter("./test-plugin-yjk.txt"));
         curThread = new Thread(new Runnable() {
            public void run() {
                while (!Thread.currentThread().isInterrupted()){
                    //System.out.println("run---");
                    try {
                        file.write("run----"+ new Date());
                        file.flush();
                    } catch (IOException e) {
                        e.printStackTrace();
                        try {
                            file.close();
                        } catch (IOException e1) {
                            e1.printStackTrace();
                        }
                    }

                    try {
                        Thread.sleep(50);
                    } catch (InterruptedException e) {
                        e.printStackTrace();
                        break;
                    }
                }
            }
        });
        curThread.start();
    }

    public void destroy() throws Exception {
        curThread.interrupt();
    }
}
